package sound;

import java.io.IOException;

import org.newdawn.slick.openal.Audio;
import org.newdawn.slick.openal.AudioLoader;
import org.newdawn.slick.util.ResourceLoader;

/**
 * OggPlayer class, handles playing audio files.
 * @author Twisty
 *
 */
public class OggPlayer {

	// The file to play in Audio object format.
	private static Audio audio;
	
	// ID of the last played audio file.
	private static int lastPlayedID;
	
	/**
	 * Play an audio file.
	 * @param musicID	The ID of the audio file.
	 */
	public static void playMusic(int musicID){
		boolean play = true;
		
		if(audio != null && audio.isPlaying()){
			play = false;
		}
		
		if(play){
			String oggPath = getAudioPath(musicID);
			try {
				audio = AudioLoader.getAudio("OGG", ResourceLoader.getResourceAsStream(oggPath));
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			if(musicID != 2){
				audio.playAsMusic(1.0f, 1.0f, false);
			}else{
				audio.playAsSoundEffect(1.0f, 1.0f, false);
			}
			lastPlayedID = musicID;
		}
		
	}
	
	/**
	 * Stop playing music.
	 */
	public static void stopMusic(){
		audio.stop();
	}
	
	/**
	 * Get the path to the audio file with given ID.
	 * @param audioID	The ID of the audio file.
	 * @return	The path to given audio file.
	 */
	private static String getAudioPath(int audioID) {
		String oggPath = "res/";
		switch(audioID){
		case 1:
			oggPath += "sm64inv.ogg";
			break;
		case 2:
			oggPath += "shot.ogg";
			break;
		case 99:
			oggPath += "test.ogg";
			break;
		}
		
		return oggPath;
	}
}
